package com.kbcss.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class MessageInsert
 */
public class MessageInsert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageInsert() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out=response.getWriter();
		//RequestDispatcher rd;
		int xx=0;
		HttpSession ss=request.getSession();
		String uid=request.getParameter("q");
		String sid=request.getParameter("p");
		String msg=request.getParameter("r");
		
		java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
	       Calendar calendar = new GregorianCalendar();
	       String am_pm;
	       int hour = calendar.get(Calendar.HOUR);
	       int minute = calendar.get(Calendar.MINUTE);
	       int second = calendar.get(Calendar.SECOND);
	       if(calendar.get(Calendar.AM_PM) == 0)
	      am_pm = "AM";
	      else
	      am_pm = "PM";
	       String date=sqlDate.toString();
	       String time=hour + ":" + minute + ":" + second + " " + am_pm;
		
		
		try{
		     Class.forName("com.mysql.jdbc.Driver");
		     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kbcssdb", "root", "mysql");
		     PreparedStatement ps=con.prepareStatement("insert into message(sendid,recvid,msgdate,msgtime,msg) values(?,?,?,?,?)");
		     ps.setString(1,uid);
		     ps.setString(2,sid);
		     ps.setString(3,date);
		     ps.setString(4,time);
		     ps.setString(5, msg);
		     xx=ps.executeUpdate();
	            if(xx==1){
	                         //con.commit();
	                         out.println(xx);
	                         //rd=request.getRequestDispatcher("sprofile.jsp");
	             			// rd.forward(request,response);
	                     }
	}
	catch(Exception ex){
		out.println("error...."+ex.getMessage());
	}
	}

	}

